package com.example.auth.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Response;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

/**
 * @author xijie.wang
 * @decription:Knife4j配置
 * @date 2022-04-11 18:55
 */
@Configuration
@EnableOpenApi
@EnableKnife4j
@ConditionalOnProperty(
		name = {"knife4j.enable"},
		havingValue = "true"
)
public class Knife4jConfig {

	@Bean
	public Docket docket() {
		return new Docket(DocumentationType.OAS_30)
				.useDefaultResponseMessages(false)
				.groupName("1.0版本")
				.select()
				//.paths(PathSelectors.any())
				.apis(RequestHandlerSelectors.basePackage("com.example.auth.rest"))
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.build()
				.directModelSubstitute(Timestamp.class, Long.class)
				.apiInfo(apiInfo())
				.ignoredParameterTypes(HttpServletRequest.class, HttpServletResponse.class)
				.globalResponses(HttpMethod.GET, getResponseMessage())
				.globalResponses(HttpMethod.POST, getResponseMessage());
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("sa-token文档")
				.description("sa-token文档")
				.version("1.0.0")
				.termsOfServiceUrl("http://localhost:9010/doc.html")
				.build();
	}

	private List<Response> getResponseMessage() {
		List<Response> resMsgList = Arrays.asList(
				new ResponseBuilder().code("200").description("成功").build(),
				new ResponseBuilder().code("401").description("无效token").build(),
				new ResponseBuilder().code("403").description("没有权限操作，请后台添加相应权限").build(),
				new ResponseBuilder().code("500").description("系统异常").build(),
				new ResponseBuilder().code("1001").description("业务异常").build(),
				new ResponseBuilder().code("1002").description("操作太频繁，请稍后再试").build());
		return resMsgList;
	}
}
